package com.product.android.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.product.android.business.ApplicationVariable;

public class SharedPreferenceHelper {

    private static final String TAG = "SharedPreferenceHelper";
    private static String PREF_NAME = "uuConfig";
    private static SharedPreferenceHelper mInstance = null;
    private Context mContext;
    protected SharedPreferences mSettings;
    protected SharedPreferences.Editor mEditor;

    // VERSION_CODE
    static public final String CONFIG_ISSHOW = "isshow"; // 是否显示
    // static public final String CONFIG_CHECKUPGRADE = "checkupgrade"; // 升级检查,应用启动时为true,成功后为false
    static public final String CONFIG_UPDATE_VER_CODE = "update_ver_code"; // 最新的版本号
    static public final String CONFIG_VERSION_CODE = "version_code"; // 要不要升级　boolean
    static public final String CONFIG_UPDATE_VER_TIME = "update_ver_time"; // 检查最新的版本时间
    static public final String CONFIG_VERSIONNAME = "versionname"; // 最新的版本号

    // UID
    static public final String CONFIG_UID = "uid";
    static public final String CONFIG_NAME = "username";
    static public final String CONFIG_UAPUID = "uapuid";
    static public final String CONFIG_ISGETAPPINFO = "isgetappinfo";
    // static public final String CONFIG_ISSYSEXISTS="issysexitsts"; //系统是否存在
    public static final String LAST_TWEET_CREATE = "_last_tweet_create_";//上次阅读的最新微博时间
    static public final String CONFIG_LOGIN_FLAG = "login_flag"; // 登录标志位

    // Group Permission
    static public final String COMMONGROUPPERMISSION = "commongrouppermission"; // 共同
    /** 添加陌生人提示配置：凡是在这个配置里出现都是不再显示添加好友提示框。*/
  	public static final String CONFIG_REMIND_FRIEND="REMIND_FRIEND_";
  	/** 公众号刚上线时显示小红点*/
  	public static final String CONFIG_PSP_NEW="CONFIG_PSP_NEW";
  	
    // 是否工号登录过
    static public final String HAS_JOB_NUMBER_LOGIN = "has_job_number_login";
    // 是否已经换过快捷回复
    static public final String HAS_UPDATE_QUICK_REPLY = "has_update_quick_reply";
   
    /**
    * Creates a new instance of SharedPreferenceUtils.
    * <br>Created 2014-8-25 下午6:38:56
    * @param c 上下文
    */
    private SharedPreferenceHelper(Context c) {
        setContext(c);
    }
    
    /**
     * 设置上下文
     * <br>Created 2014-8-25 下午6:41:46
     * @param c 上下文
     * @author       huangyx
     */
    public void setContext(Context c) {
        mContext = c;
        if (c == null) {
            LogUtils.e(TAG, "the context point is null");
            return;
        }

        mSettings = mContext.getSharedPreferences(PREF_NAME, 0);
        mEditor = mSettings.edit();
    }
    
    /**
     * 获取SharedPreference实例
     * <br>Created 2014-8-25 下午6:25:11
     * @param context 上下文
     * @return SharedPreference实例
     * @author       huangyx
     */
    public static SharedPreferenceHelper getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new SharedPreferenceHelper(context.getApplicationContext());
        }
        return mInstance;
    }

    /**
     * 获取SharedPreference实例
     * <br>Created 2014-8-25 下午6:27:36
     * @return SharedPreference实例
     * @author       huangyx
     */
    public static SharedPreferenceHelper getInstance() {
        if (mInstance == null) {
            mInstance = new SharedPreferenceHelper(ApplicationVariable.INSTANCE.applicationContext);
        }
        return mInstance;
    }

    /**
     * 提交
     * <br>Created 2014-8-25 下午6:27:55
     * @return 提交成功返回true，提交失败返回false
     * @author       huangyx
     */
    public boolean commit() {
        return mEditor.commit();
    }

    /**
     * 获取key的value值
     * <br>Created 2014-8-25 下午6:29:06
     * @param key key值
     * @return key-value对的value值
     * @author       huangyx
     */
    public String loadKey(String key) {
        return mSettings.getString(key, "");
    }

    /**
     * 保存key-value对
     * <br>Created 2014-8-25 下午6:30:59
     * @param key key值
     * @param value value值
     * @author       huangyx
     */
    public void saveKey(String key, String value) {
        mEditor.putString(key, value);
        mEditor.commit();
    }
    
    /**
     * 移除key-value对
     * <br>Created 2014-8-25 下午6:31:52
     * @param key key值
     * @author       huangyx
     */
    public void removeKey(String key) {
        mEditor.remove(key);
        mEditor.commit();
    }

    /**
     * 清除所有的key-value对
     * <br>Created 2014-8-25 下午6:32:52
     * @author       huangyx
     */
    public void clearkeys() {
        mEditor.clear();
        mEditor.commit();
    }

    /**
     * 获取boolean类型的value值
     * <br>Created 2014-8-25 下午6:33:22
     * @param key key值
     * @param defValue key不存在是的返回值
     * @return value值
     * @author       huangyx
     */
    public boolean loadBooleanKey(String key, boolean defValue) {
        return mSettings.getBoolean(key, defValue);
    }
    
    /**
     * 保存boolean类型的value值
     * <br>Created 2014-8-25 下午6:34:22
     * @param key key值
     * @param value value值
     * @author       huangyx
     */
    public void saveBooleanKey(String key, boolean value) {
        mEditor.putBoolean(key, value);
        mEditor.commit();
    }
    /**
     * 获取int类型的value值
     * <br>Created 2014-8-25 下午6:35:22
     * @param key key值
     * @param defValue key不存在是的返回值
     * @author       huangyx
     */
    public int loadIntKey(String key, int defValue) {
        return mSettings.getInt(key, defValue);
    }
    
    /**
     * 保存int类型的value值
     * <br>Created 2014-8-25 下午6:36:22
     * @param key key值
     * @param value value值
     * @author       huangyx
     */
    public void saveIntKey(String key, int value) {
        mEditor.putInt(key, value);
        mEditor.commit();
    }
    
    /**
     * 获取long类型的value值
     * <br>Created 2014-8-25 下午6:37:22
     * @param key key值
     * @param defValue key不存在是的返回值
     * @author       huangyx
     */
    public long loadLongKey(String key, long defValue) {
        return mSettings.getLong(key, defValue);
    }
    
    /**
     * 保存long类型的value值
     * <br>Created 2014-8-25 下午6:37:22
     * @param key key值
     * @param value value值
     * @author       huangyx
     */
    public void saveLongKey(String key, long value) {
        mEditor.putLong(key, value);
        mEditor.commit();
    }

   /**
    * SharedPreferences实例是否包含key
    * <br>Created 2014-8-25 下午6:39:40
    * @param key key值
    * @return SharedPreferences实例包含key返回true，否则返回false
    * @author       huangyx
    */
    public boolean isContains(String key) {
        return mSettings.contains(key);
    }

}
